// 初始数据
const initState = {
  activeId: 0,
  list: [] as Channel[],
  allList: [] as Channel[],
}

// 频道数据格式
export type Channel = {
  id: number
  name: string
}

// 频道 Action 格式
export type ChannelAction =
  | { type: 'channel/saveList'; payload: Channel[] }
  | { type: 'channel/saveAllList'; payload: Channel[] }
  | { type: 'channel/changeActiveId'; payload: number }

// 频道 reducer 纯函数
export default function channelReducer(state = initState, action: ChannelAction) {
  switch (action.type) {
    // 保存用户频道列表
    case 'channel/saveList':
      return { ...state, list: action.payload }
    // 保存所有频道列表
    case 'channel/saveAllList':
      return { ...state, allList: action.payload }
    // 更新选中频道id
    case 'channel/changeActiveId':
      return { ...state, activeId: action.payload }
    default:
      return state
  }
}
